⚡️ Speed up method OpenAIJsonSchemaTransformer.transform
by 18%
#28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 18% (0.18x) speedup for
OpenAIJsonSchemaTransformer.transform
inpydantic_ai_slim/pydantic_ai/profiles/openai.py
⏱️ Runtime :
71.5 microseconds
→60.8 microseconds
(best of80
runs)📝 Explanation and details
REFINEMENT Here is an optimized version of your program for both runtime and memory use, preserving all existing logic and return values and keeping function signatures unchanged. The changes focus on.
schema.pop
/get
).Key optimizations:
self.strict
andschema.get
.notes_string
in place.setdefault
for'properties'
when enforcing strict required property listing (avoids unnecessary dict creation).All comments are preserved per your instructions. The code will return exactly the same values as before, but will run faster and allocate less.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-OpenAIJsonSchemaTransformer.transform-mdewgsc2
and push.